package org.jeecg.client;

import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.JsonProcessingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.node.ArrayNode;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParserFactory;
import java.io.IOException;
import java.io.StringReader;
import java.util.*;
import java.util.Map.Entry;
/**
 * @author bright
 * 涓氬姟鏁版嵁闆�
 */
public class DataSet {
	private List<String> tablesName = new ArrayList<String>();
	private Map<String, DataTable> tables = new HashMap<String, DataTable>();
	
	private String name;
	
	/**
	 * 瑙ｆ瀽xml瀛楃涓插埌DataSet瀵硅薄
	 * @param str
	 * @return
	 * @throws SAXException
	 * @throws IOException
	 * @throws ParserConfigurationException
	 */
	public static DataSet ParseXml(String str) throws SAXException, IOException, ParserConfigurationException{
		final DataSet ds = new DataSet();
		StringReader sr = new StringReader(str);
		InputSource is = new InputSource(sr);
		SAXParserFactory.newInstance().newSAXParser().parse(is, new DefaultHandler(){
			private int level = 0;
			private DataRow row;
			private String column;
			@Override
			public void startElement(String uri, String localName,
					String qName, Attributes attributes) throws SAXException {
				level++;
				switch (level){
				// Table
				case 2:
					String name = qName.toLowerCase();
					if (!ds.tables.containsKey(name)){
						ds.tables.put(name, new DataTable(name));
						ds.tablesName.add(name);
					}
					DataTable dt = ds.tables.get(name);
					row = new DataRow(dt);
					dt.addRow(row);
					break;

				// Column
				case 3:
					column = qName;
					break;
				}
			}

			@Override
			public void endElement(String uri, String localName, String qName)
					throws SAXException {
				level--;
			}

			@Override
			public void characters(char[] ch, int start, int length)
					throws SAXException {
				//璁剧疆瀛楁鍊�
				if (level == 3){
					String content = new String(ch, start, length);
					row.set(column, content);
				}
			}
		});
		return ds;
	}
	
	/**
	 * json瀛楃涓插埌DataSet瀵硅薄
	 * @param str
	 * @return
	 * @throws JsonProcessingException
	 * @throws SAXException
	 * @throws IOException
	 * @throws ParserConfigurationException
	 */
	public static DataSet Parse(String str) throws JsonProcessingException, IOException{
		DataSet ds = new DataSet();
		ObjectMapper mapper = new ObjectMapper();
		JsonNode root = mapper.readTree(str);
		for(Iterator<Entry<String, JsonNode>> it = root.getFields(); it.hasNext();){
			Entry<String, JsonNode> tn = it.next();
			DataTable dt = new DataTable(tn.getKey());
			ds.tables.put(tn.getKey(), dt);
			ds.tablesName.add(tn.getKey());
			ArrayNode rowsNode = (ArrayNode)tn.getValue();
			for(Iterator<JsonNode> itr = rowsNode.iterator(); itr.hasNext(); ){
				DataRow row = new DataRow(dt);
				for(Iterator<Entry<String, JsonNode>> itf = itr.next().getFields(); itf.hasNext();){
					Entry<String, JsonNode> fn = itf.next();
					row.set(fn.getKey(), fn.getValue().getTextValue());
				}
				dt.addRow(row);
			}
		}
		return ds;
	}
	
	/**
	 * 鏍规嵁琛ㄥ悕绉拌幏鍙栨暟鎹〃
	 * @param name
	 * @return
	 */
	public DataTable getTable(String name){
		name = name.toLowerCase();
		return tables.containsKey(name) ? tables.get(name) : null;
	}
	
	/**
	 * 鏍规嵁绱㈠紩鑾峰彇琛�
	 * @param idx
	 * @return
	 */
	public DataTable getTable(int idx){
		return tables.get(tablesName.get(idx));
	}
/*	
	public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException, ParseException{
		byte b = -60;
		System.out.println(b & 0xff);
		String xml = 
"{\"Table1\":[{\"WO_ID\":\"00000003-3\",\"PARTNO\":\"GU10R1*3WWH25N61N16\",\"PARTVER\":\"0\",\"LINENAME\":\"DEMOLINE\",\"LINEGRP\":null,\"DESCRIPTION\":\"\",\"SPEC\":\"SPEC01\",\"PLAN_START\":\"2014-03-13T00:00:00\",\"RLS_DATE\":\"2014-05-30T21:18:30\",\"RLS_QTY\":2007.000000,\"INPUT_QTY\":0.000000,\"OUTPUT_QTY\":0.000000,\"CONFIRM_QTY\":0.000000,\"INV_QTY\":0.000000,\"NG_QTY\":0.000000,\"DYNA_INFO\":\"\",\"COMMENTS\":\"\"},{\"WO_ID\":\"00000008-3\",\"PARTNO\":\"B35AJ4WWH230L11DE129\",\"PARTVER\":\"0\",\"LINENAME\":\"DEMOLINE\",\"LINEGRP\":null,\"DESCRIPTION\":\"\",\"SPEC\":\"SPEC01\",\"PLAN_START\":\"2014-03-05T00:00:00\",\"RLS_DATE\":\"2014-05-30T21:18:30\",\"RLS_QTY\":2500.000000,\"INPUT_QTY\":1.000000,\"OUTPUT_QTY\":0.000000,\"CONFIRM_QTY\":0.000000,\"INV_QTY\":0.000000,\"NG_QTY\":0.000000,\"DYNA_INFO\":\"\",\"COMMENTS\":\"\"},{\"WO_ID\":\"00000009-3\",\"PARTNO\":\"B35AJ7WWH00L11CE129\",\"PARTVER\":\"0\",\"LINENAME\":\"DEMOLINE\",\"LINEGRP\":null,\"DESCRIPTION\":\"\",\"SPEC\":\"SPEC01\",\"PLAN_START\":\"2014-03-05T00:00:00\",\"RLS_DATE\":\"2014-05-30T21:18:29\",\"RLS_QTY\":500.000000,\"INPUT_QTY\":0.000000,\"OUTPUT_QTY\":0.000000,\"CONFIRM_QTY\":0.000000,\"INV_QTY\":0.000000,\"NG_QTY\":0.000000,\"DYNA_INFO\":\"\",\"COMMENTS\":\"\"},{\"WO_ID\":\"00000010-3\",\"PARTNO\":\"MR16EX6WW36I33BE129\",\"PARTVER\":\"0\",\"LINENAME\":\"DEMOLINE\",\"LINEGRP\":null,\"DESCRIPTION\":\"\",\"SPEC\":\"SPEC01\",\"PLAN_START\":\"2014-03-05T00:00:00\",\"RLS_DATE\":\"2014-05-30T21:18:32\",\"RLS_QTY\":5400.000000,\"INPUT_QTY\":0.000000,\"OUTPUT_QTY\":0.000000,\"CONFIRM_QTY\":0.000000,\"INV_QTY\":0.000000,\"NG_QTY\":0.000000,\"DYNA_INFO\":\"\",\"COMMENTS\":\"\"},{\"WO_ID\":\"14040020-1\",\"PARTNO\":\"SDTL001RN336CE0035\",\"PARTVER\":\"0.0\",\"LINENAME\":\"test\",\"LINEGRP\":null,\"DESCRIPTION\":null,\"SPEC\":\"SPEC01\",\"PLAN_START\":\"2014-03-27T00:00:00\",\"RLS_DATE\":\"2014-06-09T16:17:19\",\"RLS_QTY\":28.000000,\"INPUT_QTY\":4390.000000,\"OUTPUT_QTY\":322.000000,\"CONFIRM_QTY\":13.000000,\"INV_QTY\":0.000000,\"NG_QTY\":0.000000,\"DYNA_INFO\":\"\",\"COMMENTS\":\"\"},{\"WO_ID\":\"testwyh1\",\"PARTNO\":\"GU10AB6*1CW36L33E129\",\"PARTVER\":\"0\",\"LINENAME\":\"DEMOLINE\",\"LINEGRP\":null,\"DESCRIPTION\":\"\",\"SPEC\":\"SPEC01\",\"PLAN_START\":\"2014-06-11T16:05:58\",\"RLS_DATE\":\"2014-06-11T16:05:58\",\"RLS_QTY\":1000.000000,\"INPUT_QTY\":1.000000,\"OUTPUT_QTY\":0.000000,\"CONFIRM_QTY\":0.000000,\"INV_QTY\":0.000000,\"NG_QTY\":0.000000,\"DYNA_INFO\":\"\",\"COMMENTS\":\"\"}]}";
		DataSet ds = Parse(xml);
		Date  t = ds.getTable(0).getRows().get(5).getDate("PLAN_START");
	}
*/

	public List<String> getTablesName() {
		return tablesName;
	}

	public void setTablesName(List<String> tablesName) {
		this.tablesName = tablesName;
	}

	public Map<String, DataTable> getTables() {
		return tables;
	}

	public void setTables(Map<String, DataTable> tables) {
		this.tables = tables;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
}
